home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1996 #6
/
Amiga Plus CD - 1996 - No. 06.iso
/
pd
/
netz
/
dl
/
misc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-02
|
5KB
|
252 lines
/* Copyright (C) 1995, 1996 Zlatko Calusic <maverick@fly.cc.fer.hr>
This file is part of DownLoad, FTP Client
The DownLoad is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation.
The DownLoad is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
the DownLoad source; see the file COPYING. If not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <math.h>
#include "dl.h"
extern int local;
extern int parsed;
extern char url[], host[], path[], file[];
char *rate(int bytes, int seconds)
{
int flag = 0;
char m_q[16], m_e[16], m_t[16];
double quantity, elapsed, transfer;
static char line[256];
*line = 0;
if (bytes <= 0 || seconds < 0)
return line;
if (!seconds)
seconds++;
if (bytes < 1000)
{
quantity = bytes;
strcpy(m_q, "bytes");
flag |= 1;
}
else if (bytes >= 1000 && bytes < 1000000)
{
quantity = (double) bytes / 1000;
strcpy(m_q, "Kbytes");
}
else
{
quantity = (double) bytes / 1000000;
strcpy(m_q, "Mbytes");
}
if (seconds < 60)
{
elapsed = seconds;
strcpy(m_e, "seconds");
flag |= 2;
}
else if (seconds >= 60 && seconds < 3600)
{
elapsed = (double) seconds / 60;
strcpy(m_e, "minutes");
}
else
{
elapsed = (double) seconds / 3600;
strcpy(m_e, "hours");
}
transfer = (double) bytes / seconds;
if (transfer < 1000)
strcpy(m_t, "bytes/sec");
else if (transfer > 1000 && transfer < 1000000)
{
transfer /= 1000;
strcpy(m_t, "Kbytes/sec");
}
else
{
transfer /= 1000000;
strcpy(m_t, "Mbytes/sec");
}
switch (flag)
{
case 0:
sprintf(line, "Received %.2f %s in %.2f %s (%.2f %s).\n", quantity, m_q, elapsed, m_e, transfer, m_t);
break;
case 1:
sprintf(line, "Received %d %s in %.2f %s (%.2f %s).\n", (int) quantity, m_q, elapsed, m_e, transfer, m_t);
break;
case 2:
sprintf(line, "Received %.2f %s in %d %s (%.2f %s).\n", quantity, m_q, (int) elapsed, m_e, transfer, m_t);
break;
case 3:
sprintf(line, "Received %d %s in %d %s (%.2f %s).\n", (int) quantity, m_q, (int) elapsed, m_e, transfer, m_t);
}
return line;
}
int parse(void)
{
char *cptr;
int last = 0;
if ((cptr = strchr(url, ' ')))
*cptr = ':';
while (*(strchr(url, 0) - last - 1) == ':')
last++;
if (strstr(url, "ftp://") == url) /* line is in URL format */
{
strcpy(url, url + 6);
if ((cptr = strchr(url, '/')))
*cptr = ':';
}
if ((cptr = strtok(url, ":")))
strcpy(host, cptr);
else
return ERROR; /* ":" not found */
if ((cptr = strtok(NULL, ":")))
strcpy(path, cptr);
else
return ERROR;
while((cptr = strtok(NULL, ":")))
{
strcat(path, ":");
strcat(path, cptr);
}
while (last)
{
strcat(path, ":");
last--;
}
if (*(cptr = strchr(path, 0) - 1) == '/')
return ERROR; /* last character is "/" */
while (*--cptr != '/')
if (cptr < path)
{ /* "/" not found */
cptr = path - 1;
break;
}
strcpy(file, cptr + 1);
if (cptr > path)
*cptr = 0;
else
*path = 0;
parsed = 1;
return OK;
}
/* Revised fnmatch.c, originally written by Brian Fox & Chet Ramey */
int match(char *pattern, char *string, int flag)
{
char ch, *p = pattern, *n = string;
while ((ch = *p++))
{
switch (ch)
{
case '?':
if (!*n || (flag && *n == '.' && n == string))
return ERROR;
break;
case '\\':
if (*n != (ch = *p++))
return ERROR;
break;
case '*':
if (flag && *n == '.' && n == string)
return ERROR;
for (ch = *p++; ch == '?' || ch == '*'; ch = *p++, ++n)
if (ch == '?' && !*n)
return ERROR;
if (!ch)
return OK;
{
char ch1 = (ch == '\\') ? *p : ch;
for (--p; *n; ++n)
if ((ch == '[' || *n == ch1) && match (p, n, (flag ? 0 : 1)) == OK)
return OK;
return ERROR;
}
case '[':
{
int not;
if (!*n || (flag && *n == '.' && n == string))
return ERROR;
{
char *np;
for (np = p; np && *np && *np != ']'; np++);
if (np && !*np)
{
if (*n != '[')
return ERROR;
goto next_char;
}
}
if ((not = (*p == '!' || *p == '^')))
++p;
ch = *p++;
for (;;)
{
char start = ch, end = ch;
if (ch == '\\')
start = end = *p++;
if (!ch)
return ERROR;
ch = *p++;
if (ch == '-' && *p != ']')
{
if ((end = *p++) == '\\')
end = *p++;
if (!end)
return ERROR;
ch = *p++;
}
if (*n >= start && *n <= end)
goto matched;
if (ch == ']')
break;
}
if (!not)
return ERROR;
matched:
while (ch != ']')
{
if (!ch)
return ERROR;
if ((ch = *p++) == '\\')
++p;
}
if (not)
return ERROR;
}
next_char:
break;
default:
if (ch != *n)
return ERROR;
}
++n;
}
if (!*n)
return OK;
else
return ERROR;
}